package com.sentrix.auto.printercenter.infrastructure.erp;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.sentrix.auto.printercenter.domain.vo.ErpOrderPrintInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ErpClient {

    private final String erpBaseUrl;
    private final Gson gson = new Gson();

    public ErpClient(@Autowired @Value("${url:666}") String erpBaseUrl) {
        this.erpBaseUrl = erpBaseUrl;
    }

    public ErpOrderPrintInfo queryOrderPrintInfo(String orderCode) {
        String fullUrl = erpBaseUrl + "/order/print/" + orderCode;
        try (HttpResponse response = HttpRequest.post(fullUrl).execute()) {
            if (!response.isOk()) {
                throw new RuntimeException("Request Failed");
            }
            //response.header("Content-Type");
            String body = response.body();
            return gson.fromJson(body, ErpOrderPrintInfo.class);
        }
    }
}
